package com.zhhs.galaxyott.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zhhs.galaxyott.R;

/**
 * 
 * @author lc
 * @created 2015年12月23日
 * @category Toast、AlertDialog
 * 
 */
@SuppressLint("NewApi")
public class PromptsUtils {

    private static Toast mToast;

    /**
     * Toast显示消息
     * 
     * @param activity
     * @param message
     */
    public static final void showToast(final Activity activity, final String message) {
        if (activity != null)
            showToast(activity, message, null);
    }

    public static final void showToast(final Fragment fragment, final String message) {
        if (fragment != null)
            showToast(fragment.getActivity(), message, null);
    }

    public static final void showCustomToast(final Activity activity, final String message) {
        if (activity != null)
        // showToast(activity, message, null);
        {
            LayoutInflater inflater = LayoutInflater.from(activity);
            View view = inflater.inflate(R.layout.toast_layout, null);
            ((TextView) (view.findViewById(R.id.toast_txt))).setText(message);

            Toast toast = new Toast(activity);
            toast.setGravity(Gravity.BOTTOM, 0, 120);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(view);
            toast.show();
        }

    }

    /**
     * 打印消息并且用Toast显示消息
     * 
     * @param activity
     * @param message
     * @param logLevel
     *            填d, w, e分别代表debug, warn, error; 默认是debug
     */
    public static final void showToast(final Activity activity, final String message, String logLevel) {
        if ("w".equals(logLevel)) {
            Log.w("sdkDemo", message);
        } else if ("e".equals(logLevel)) {
            Log.e("sdkDemo", message);
        } else {
            Log.d("sdkDemo", message);
        }
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                if (mToast != null) {
                    mToast.cancel();
                    mToast = null;
                }
                mToast = Toast.makeText(activity, message, Toast.LENGTH_SHORT);
                mToast.show();
            }
        });
    }

    /**
     * 
     * @param context
     * @param msg
     * @param listener
     */
    public static final void showAlertDlg(Context context, String msg, OnClickListener listener) {
        new AlertDialog.Builder(context).setMessage(msg).setCancelable(false).setPositiveButton("确认", listener)
                .setNegativeButton("取消", null).create().show();
    }

    /**
     * 
     * @param context
     * @param msg
     * @param posListener
     * @param negListener
     */
    public static final void showAlertDlg(Context context, String msg, OnClickListener posListener,
            OnClickListener negListener) {
        new AlertDialog.Builder(context).setMessage(msg).setPositiveButton(android.R.string.ok, posListener)
                .setNegativeButton(android.R.string.cancel, negListener).setCancelable(false).create().show();
    }

}
